/*
 * fcaStorePlace.java
 *
 * Created on 25. leden 2004, 22:48
 */

package cz.masa.FotoCAT;

import java.sql.*;
import java.util.*;

/**
 *
 * @author  Jiri Masnik
 */
public class fcaDirectory {
    private int             db_id;
    private int             db_id_store;
    private String          db_name;
    private String          db_description;
    private String          db_directory;
    private java.sql.Date   db_date_created;
    private java.sql.Date   db_date_from;
    private java.sql.Date   db_date_to;

    
    private static final String MAIN_SQL_FLDS = "fca04_directory.fca04_id, fca04_directory.fca04_directory, fca04_directory.fca02_id, fca04_directory.fca04_name, fca04_directory.fca04_description, fca04_directory.fca04_created, fca04_directory.fca04_date_from, fca04_directory.fca04_date_to";
    public static String getMainSQLFlds(){ return fcaDirectory.MAIN_SQL_FLDS; }
    private static final String MAIN_SQL = "SELECT " + fcaDirectory.MAIN_SQL_FLDS + "," + fcaStorePlace.getMainSQLFlds() + " FROM fca04_directory LEFT OUTER JOIN fca02_store ON fca02_store.fca02_id = fca04_directory.fca02_id";
    public static String getMainSQL(){ return fcaDirectory.MAIN_SQL; }
    
    private fcaStorePlace fcaStore; 
    
    public Vector images;
    
    /** Creates a new instance of fcaStorePlace */
    public fcaDirectory() {
        this.db_id = -1;
        fcaStore = new fcaStorePlace();
        images = new Vector();
    }

    public fcaDirectory(int db_id) {
        fcaStore = new fcaStorePlace();
        dbLoad(db_id);
    }
    
    /**
     * Nacteni seznamu fotek, ktere dir obsahuje
     */
    public int loadImages(){
        int ret = 0;
        
        // vycistime seznam
        images.clear();
        
        try {
            // SQL, dotaz
            ResultSet rs;
            String sql = fcaImage.getMainSQL() + " WHERE fca03_picture.fca04_id = " + this.db_id;
            rs = SQLCon.exSQLres(sql);
        
            // nacteni, vytvoreni class
            while (rs.next()) {
                fcaImage fi = new fcaImage();
                if(fi.dbLoad(rs) == 1){
                    images.add(fi);
                    ret++;
                }
            }
            rs.close();
        } catch(SQLException ex) {
            System.err.println("Nacitani seznamu obrazku: " + ex);
        }            
        
        return ret;
    }
    
    /**
     * Cela cesta k danemu adresari
     */
    public String getFullPath(){
        String ret_path = fcaStore.getFullPath() + db_directory + FotoCAT.file_sep;
        return ret_path;
    }    
    
    /**
     * Nacteni na zaklade ID
     */
    public int dbLoad(int db_id) {
        ResultSet rs = SQLCon.exSQLres(fcaDirectory.MAIN_SQL + " WHERE fca04_directory.fca04_id = " + db_id);
        try {
            rs.first();
            dbLoad(rs);
        } catch(SQLException ex) { 
            return 0;
        }
        return 1;
    }
    
    /**
     * Nacteni z DB
     */
    public int dbLoad(ResultSet rs){
        try {
            this.db_id = rs.getInt("fca04_directory.fca04_id");
            this.db_id_store = rs.getInt("fca04_directory.fca02_id");
            this.db_name = rs.getString("fca04_directory.fca04_name");
            this.db_description = rs.getString("fca04_directory.fca04_description");
            this.db_directory = rs.getString("fca04_directory.fca04_directory");
            this.db_date_created = rs.getDate("fca04_directory.fca04_created");
            this.db_date_from = rs.getDate("fca04_directory.fca04_date_from");
            this.db_date_to = rs.getDate("fca04_directory.fca04_date_to");
            
            // nacteni clasy uloziste
            if(fcaStore.dbLoad(rs) != 1) return 0;
            
            return 1;
        }
        catch(SQLException ex) {
            return 0;
        }
    }
    
    /**
     * Odstraneni sama sebe z DB
     */
    public void dbDelete(){
        if(db_id != -1){
            SQLCon.exSQL("DELETE FROM fca04_directory WHERE fca04_id = " + db_id);
        }
    }
    
    /**
     * Odstraneni vsech obrazku z DB
     */
    public void dbDeleteImages(){
        if(db_id != -1){
            SQLCon.exSQL("DELETE FROM fca03_picture WHERE fca04_id = " + db_id);
        }
    }
    
    /**
     * Ulozeni dat do DB
     */
    public int dbSave() {
        boolean was_insert = false;
        Connection con = SQLCon.getConnection();
        try {            
            PreparedStatement pStatement;
            if(db_id == -1){
                pStatement = con.prepareStatement("INSERT INTO fca04_directory (fca04_name, fca04_description, fca04_created, fca04_date_from, fca04_date_to, fca02_id, fca04_directory) VALUES (?,?,NOW(),?,?,?,?)");
                pStatement.setString(1, db_name);
                pStatement.setString(2, db_description);
                pStatement.setDate(3, db_date_from);
                pStatement.setDate(4, db_date_to);
                pStatement.setInt(5, db_id_store);
                pStatement.setString(6, db_directory);
                was_insert = true;
            } else {
                pStatement = con.prepareStatement("UPDATE fca04_directory SET fca04_name = ?, fca04_description = ?, fca04_date_from = ?, fca04_date_to = ?, fca02_id = ?, fca04_directory = ? WHERE fca04_id = ?");
                pStatement.setString(1, db_name);
                pStatement.setString(2, db_description);
                pStatement.setDate(3, db_date_from);
                pStatement.setDate(4, db_date_to);
                pStatement.setInt(5, db_id_store);
                pStatement.setString(6, db_directory);
                pStatement.setInt(7, db_id);
            }
            pStatement.executeUpdate();
        }
        catch(Exception ex){
            System.out.println(ex);
            return 0;
        }
        
        // pokud to byl insert, znovunacteni objektu
        if(was_insert == true) {
            ResultSet rs = SQLCon.exSQLres("SELECT MAX(fca04_id) AS newid FROM fca04_directory");
            try {
                rs.first();
                int new_id = rs.getInt("newid");
                dbLoad(new_id);
            } catch(SQLException ex) {
                return 0;
            }
        }
        
        return 1;
    }    
    
    /**
     * Nastaveni skupiny, do ktere adresar patri
     */
    public int setGroup(int group_id){
        Connection con = SQLCon.getConnection();
        try {
            PreparedStatement pStatement;
            if(group_id == -1){
                pStatement = con.prepareStatement("UPDATE fca04_directory SET fca01_id = NULL WHERE fca04_id = ?");
                pStatement.setInt(1, db_id);
            } else {
                pStatement = con.prepareStatement("UPDATE fca04_directory SET fca01_id = ? WHERE fca04_id = ?");
                pStatement.setInt(1, group_id);
                pStatement.setInt(2, db_id);
            }
            pStatement.executeUpdate();
        }
        catch(Exception ex){
            System.out.println(ex);
            return 0;
        }
        return 1;        
    }
    
    public fcaStorePlace getFcaStore(){
        return this.fcaStore;
    }
    
    public String getName(){
        return db_name;
    }
    
    public int setName(String name){
        if(name.compareTo("") == 0) name = "(no name)";
        db_name = name;
        return 1;
    }
    
    public String getDirectory(){
        return db_directory;
    }
    
    public int setDirectory(String dir){
        db_directory = dir;
        return 1;
    }
    
    public String getDescription(){
        return db_description;
    }
    
    public int setDescription(String description){
        db_description = description;
        return 1;
    }
    
    public int getId(){
        return db_id;
    }
    
    public int getIdStore(){
        return db_id_store;
    }
    
    public int setIdStore(int id){
        db_id_store = id;
        fcaStore = new fcaStorePlace(id);
        return 1;
    }
    
    public java.util.Date getDateCreated(){
        return db_date_created;
    }
    
    public java.util.Date getDateFrom(){
        return db_date_from;
    }
    
    public int setDateFrom(java.sql.Date date){
        db_date_from = date;
        return 1;
    }
    
    public java.util.Date getDateTo(){
        return db_date_to;
    }
    
    public int setDateTo(java.sql.Date date){
        db_date_to = date;
        return 1;
    }
    
    /**
     * Objem dat v albu
     */
    public long getDataAmount(){
        long ret;
        ResultSet rs = SQLCon.exSQLres("SELECT SUM(fca03_filesize) AS size FROM fca03_picture WHERE fca04_id =" + db_id);
        try {
            rs.first();
            ret = rs.getLong("size");
        } catch(SQLException ex) {
            return 0;
        }
        return ret;
    }
    
    /**
     * Pocet obrazku v albu
     */
    public int getImagesCount(){
        ResultSet rs = SQLCon.exSQLres("SELECT COUNT(*) AS count FROM fca03_picture WHERE fca04_id = " + db_id);
        try {
            rs.first();
            return rs.getInt("count");
        }
        catch(SQLException ex) {
            return -1;
        }
    }
    
    /**
     * Test zda soubor s danym jmenem souboru jiz v DB je
     */
    public boolean haveFilename(String filename){
        ResultSet rs = SQLCon.exSQLres("SELECT fca03_id FROM fca03_picture WHERE fca03_filename LIKE '" + filename + "' AND fca04_id = " + db_id);
        try {
            if(rs.next()) return true;
        } catch(SQLException ex) { }
        return false;
    }

}